// 显示图片
export function imgPreview(id: any) {
  const download = '/dev-api/system/oss/preview?ossId=';
  return download + id;
}

export const getLabel = (options: string | any[], value: any) => {
  for (let i = 0; i < options.length; i++) {
    if (options[i].value === value) {
      return options[i].label;
    }
  }
  return '-';
};

/**
 * 获取options的item
 * @param {*} options 目标options
 * @param {*} value 某一项的value值
 * @param {*} key 匹配项的某一属性
 * @returns
 */
export const getItem = (
  options: string | any[],
  value: any,
  key: string | number,
) => {
  for (let i = 0; i < options?.length; i++) {
    if (options[i].value === value) {
      return key ? options[i][key] : options[i]['label'];
    }
  }
  return '-';
};

export const getField = (
  options: string | any[],
  value: any,
  field: string | number,
) => {
  for (let i = 0; i < options.length; i++) {
    if (options[i].value === value) {
      return options[i]?.[field];
    }
  }
  return '-';
};

// setTimeout promise版
export const sleep = (time: number | undefined) =>
  new Promise((resolve: any) => {
    if (time) {
      setTimeout(() => {
        resolve();
      }, time);
    } else {
      resolve();
    }
  });

/**
 * 枚举类
 */
class EnumArray extends Array {
  getField(val: any, field = 'label') {
    return this.find((item) => item?.value === val)?.[field] || '-';
  }
}
export function createEnum(enums: any) {
  return Object.freeze(new EnumArray(...enums));
}
